

jQuery(function ($) {
	//禁止输入中文
	var re = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/g;
	var reg = /^1[3456789]\d{9}$/;
	var user = JSON.parse(localStorage.getItem('user_info')) || {};
	var token = user.token;
	var reg_email = /^\w+\@+[0-9a-zA-Z]+\.(com|com.cn|edu|hk|cn|net)$/;
	$('#tel').on('input', function () {
		var value = $(this).val();
		var re = /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/g;
		this.value = value.replace(re, '');
	});
	//验证用户名的格式
	$("#tel").blur(function () {
		if (reg.test($("#tel").val())) {
			if (reg.test($("#tel").val())) {
				$("#input-box").css("display", "block");
			} else {
				$("#input-box").css("display", "none");
			}
			$(".tel-msg").text('');
			$('#input_code').removeClass('hide');
			change_pic();
		} else {
			$(".tel-msg").text('请输入正确的手机号');
			$('#input_code').addClass('hide');
			$('#text').val('');
			return;
		}
		if ($("#tel").val() == '') {
			$(".tel-msg").text('对不起，账号不能为空');
			$('#input_code').addClass('hide');
			$('#text').val('');
			return;
		}
	});
	//密码长度
	$("#code").blur(function () {
		var password = $.trim($("#code").val());
		if (password.length < 6) {
			$(".code-msg1").html("密码不能少于六位数");
			return;
		} else {
			$(".code-msg1").html("");
		}
	});
	//验证密码是否相同
	$("#code2").blur(function () {
		var password = $.trim($("#code").val());
		var password2 = $.trim($("#code2").val());
		if (password != password2) {
			$(".code-msg").html('两次密码输入不相同');
			return;
		} else {
			$(".code-msg").html('');
		}
	});
	//验证码
	//更新图片
	$('#change_pic').click(function () {
		change_pic();
	});
	function change_pic() {
		var tel = $.trim($("#tel").val()); //账号
		var img = document.querySelector('.text_pic');
		img.src = host + "/api/code/img?phone=" + tel;
	};
	document.querySelector('.qq').href = host + "/qqLogin";
	document.querySelector('.weixin').href = host + "/weixinLogin";
	//注册
	$(".btn").click(function () {
		var loginCode = $.trim($("#tel").val()); //账号
		var password = $.trim($("#code").val()); //密码
		var password2 = $.trim($("#code2").val());
		// var textCode = $.trim($('#text').val()); //图形验证码
		var textCode2 = $.trim($('#text2').val()); //短信验证码
		var data = {
			"phone": loginCode,
			"password": password,
			"code": textCode2
			//非空验证
		};if (loginCode == "" || loginCode == null) {
			$(".tel-msg").html("对不起，账号不能为空");
			return;
		} else if (password == "" || password == null) {
			$("#code").focus();
			$(".code-msg").html("对不起，登录密码不能为空");
			return;
		}

		if (reg.test($("#tel").val()) && password == password2) {
			if (textCode2 == "" || textCode2 == null) {
				$(".text-msg2").html("请输入验证码");
				return;
			}
			$.ajax({
				type: 'POST', //提交方法是POST
				url: host + '/api/user/register', //请求的路径
				data: JSON.stringify(data),
				contentType: "application/json",
				xhrFields: {
					withCredentials: true
				},
				beforeSend: function beforeSend(request) {
					request.setRequestHeader("X-AUTH-TOKEN", token);
				},
				error: function error(_error) {
					//请求失败的回调方法 
					alert('网络加载失败，请稍候再试!');
				},
				success: function success(result) {
					if (result.code == 200) {
						var id = result.data.id;
						var uid = result.data.uid;
						var user_name = result.data.loginName;
						var point = result.data.point;
						var levelId = result.data.levelId;
						var balance = result.data.balance;
						var growth = result.data.growth;
						var data = {
							"id": id,
							"uid": uid,
							"user_name": user_name,
							"point": point,
							"token": result.attributes.token,
							"level_id": levelId,
							"balance": balance,
							"growth": growth
						};
						localStorage.setItem("user_info", JSON.stringify(data));
						var isStoreGoods = JSON.parse(localStorage.getItem('noUsers')) || {};
						if (isStoreGoods.length) {
							var goodsIdArr = [];
							for (var i = 0; i < isStoreGoods.length; i++) {
								goodsIdArr.push(isStoreGoods[i].id);
							};
							var data = {
								"activity_id": 0,
								'user_id': id,
								'goods_ids': goodsIdArr,
								type: 1
							};
							send_ajax('post', '/api/cart/add', data, function callback(result) {
								if (result.code == 200) {
									window.localStorage.removeItem('noUsers');
									var prevLink = document.referrer;
									var url = window.location.host;
									if ($.trim(prevLink) == '') {
										location.href = '/';
									} else {
										if (prevLink.indexOf(url) == -1) {
											//来自其它站点
											location.href = '/';
											return;
										}
										if (prevLink.indexOf('/login') != -1) {
											//来自注册页面
											location.href = '/';
											return;
										}
										location.href = prevLink;
									}
								}
							});
						} else {
							var prevLink = document.referrer;
							var url = window.location.host;
							if ($.trim(prevLink) == '') {
								location.href = '/';
							} else {
								if (prevLink.indexOf(url) == -1) {
									//来自其它站点
									location.href = '/';
									return;
								}
								if (prevLink.indexOf('/login') != -1) {
									//来自注册页面
									location.href = '/';
									return;
								}
								location.href = prevLink;
							}
						};
					} else {
						$(".tel-msg").html(result.msg);
						change_pic();
					}
				}
			});
		} else if (reg_email.test($("#tel").val()) && password == password2) {
			$.ajax({
				type: 'POST', //提交方法是POST
				url: host + '/api/user/email_register', //请求的路径
				data: JSON.stringify({ email: loginCode, password: password }),
				contentType: "application/json",
				xhrFields: {
					withCredentials: true
				},
				beforeSend: function beforeSend(request) {
					request.setRequestHeader("X-AUTH-TOKEN", token);
				},
				error: function error(_error2) {
					//请求失败的回调方法 
					alert('网络加载失败，请稍候再试!');
				},
				success: function success(result) {
					if (result.code == 200) {
						window.location.href = "mail_verify.html?type=1&&email=" + loginCode;
					} else {
						$(".tel-msg").html(result.msg);
						change_pic();
					}
				}
			});
		} else {
			alert('格式错误');
		}
	});
	var time = 60;
	var flag = true;
	var timer;
	//获取图形验证码
	$('.getcode').click(function () {
		var tel = $.trim($("#tel").val());
		var textCode = $.trim($('#text').val()); //图形验证码 
		if (!textCode) {
			$(".tel-msg").html("请输入图形验证码！");
			return;
		}
		if (!tel) {
			$(".tel-msg").html("对不起，账号不能为空");
			return;
		} else if (!reg.test($("#tel").val())) {
			$(".tel-msg").text('请输入正确的手机号或邮箱地址');
			return;
		} else {
			$(".tel-msg").text('');
			var data = {
				phone: tel,
				imgCode: textCode,
				type: 1
			};
			$.ajax({
				type: 'POST', //提交方法是POST
				url: host + '/api/code/sms', //请求的路径
				data: JSON.stringify(data),
				contentType: "application/json",
				beforeSend: function beforeSend(request) {
					request.setRequestHeader("X-AUTH-TOKEN", token);
				},
				error: function error(_error3) {
					//请求失败的回调方法 
					alert('网络加载失败，请稍候再试!');
				},
				success: function success(result) {
					if (result.code == 200) {
						$("#btn-code").attr("disabled", true);
						if (flag == true) {
							flag = false;
							timeone();
						}
					} else {
						alert('网络加载失败，请稍候再试!');
					}
				}
			});
		}
	});
	function timeone() {
		$('#btn-code').html("(" + time + ")后重新发送");
		timer = setInterval(function () {
			time--;
			if (time == 0) {
				$("#btn-code").attr("disabled", false);
				$("#btn-code").html("点击获取验证码");
				time = 60;
				flag = true;
				clearInterval(timer);
			} else {
				$('#btn-code').html("(" + time + ")后重新发送");
			}
		}, 1000);
	}
	$(".header-r").click(function () {
		window.location.href = "login.html";
	});

	$(document).keydown(function (event) {
		if (event.keyCode == 13) {
			$('.btn').triggerHandler('click');
		}
	});
});